package com.yx.yinshanjinniu.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.TimeCount;
import com.yx.yinshanjinniu.utils.ToastUtil;
import com.yx.yinshanjinniu.view.MyEditText;


public class FindBackPwdActivity extends BaseActivity implements View.OnClickListener {
    private MyEditText met_mobile;
    private MyEditText met_password;
    private MyEditText et_code;
    private TextView tv_get_code;
    private Button bt_submit;
    private String mobile = "";
    private TimeCount timeCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        met_mobile.showSoftKeyboard();
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_find_back_pwd;
    }

    @Override
    protected void findViews() {
        met_mobile = (MyEditText) findViewById(R.id.met_mobile);
        met_password = (MyEditText) findViewById(R.id.met_pwd);
        et_code = (MyEditText) findViewById(R.id.et_code);
        tv_get_code = (TextView) findViewById(R.id.tv_get_code);
        bt_submit = (Button) findViewById(R.id.bt_submit);
    }

    @Override
    protected void setListeners() {
        tv_get_code.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_get_code:
                getCode();
                break;
            case R.id.bt_submit:
                submit();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount != null) {
            timeCount.finishTimeCount();
            timeCount = null;
        }
    }

    private void getCode() {
        loading();
        mobile = met_mobile.getText();
        mAppAction.resetPasswordCode(mobile, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                cancelLoading();
                timeCount = new TimeCount(tv_get_code, 60000, 1000, "再次获取");
                timeCount.start();
                ToastUtil.showToast(mContext, "验证码发送成功");
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    private void submit() {
        loading();
        mobile = met_mobile.getText();
        mAppAction.resetPassword(mobile, et_code.getText().toString(), met_password.getText(),
                new ActionCallbackListener<Void>() {
                    @Override
                    public void onSuccess(Void data) {
                        cancelLoading();
                        ToastUtil.showToast(mContext, "新密码设置成功");
                        backActivity();
                    }

                    @Override
                    public void onFailure(String errorCode, String message) {
                        cancelLoading();
                        ToastUtil.showToast(mContext, message);
                    }
                });
    }
}
